//
//  SquareVC.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SquareVC.h"
#import "ZWBSquareCell.h"
#import "SquareModel.h"
#import "TimeTool.h"
#import "ZWBWebVC.h"

@interface SquareVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *SquareTabView;
@property(nonatomic, strong)NSMutableArray *SquareArr;
@property(nonatomic, assign)BOOL isUpLoading;// 判断下拉
@property(nonatomic, copy)NSString *str;
@end

@implementation SquareVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self createData];
}


- (void)createTableView {
    
    self.SquareTabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT - 49)style:UITableViewStylePlain];
    [self.view addSubview:_SquareTabView];
    
    _SquareTabView.delegate = self;
    _SquareTabView.dataSource = self;
    _SquareTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.SquareTabView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.str = @"";
        [self createData];
        
    }];
    
    
    self.SquareTabView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self createData];
        
    }];

    
}

- (void)createData {
    
    if ([self.SquareTabView.footer isRefreshing]) {
        
        self.str = [NSString stringWithFormat:@"&ver=%@", [[self.SquareArr lastObject] ver]];
        NSString *str = [NSString stringWithFormat:@"http://api.huapipi.com/recommend/pipi?deviceId=1826171&uid=1033275%@", self.str];
        [ToolNetWorking ToolNetWorking:str block:^(id result) {
            
            [self.SquareArr addObjectsFromArray:[SquareModel BaseModel:result[@"rows"]]];
            
            if (self.SquareArr.count > 0) {
                [self.SquareTabView reloadData];
                [self.SquareTabView.header endRefreshing];
                [self.SquareTabView.footer endRefreshing];
            }
        }];
    } else {
        [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/recommend/pipi?deviceId=1826171&uid=1033275" block:^(id result) {
            
            self.SquareArr = [NSMutableArray array];
            self.SquareArr = [SquareModel BaseModel:result[@"rows"]];
            
            if (self.SquareArr.count > 0) {
                [self.SquareTabView reloadData];
                [self.SquareTabView.header endRefreshing];
                [self.SquareTabView.footer endRefreshing];
            }
        }];

    }
   
}



#pragma mark tableView dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuse = @"reuse";
    ZWBSquareCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[ZWBSquareCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SquareModel *model = self.SquareArr[indexPath.row];
    if ([model.ftype isEqualToString:@"text"]) {
        
        
        cell.typeImageView.image = [UIImage imageNamed:@"iconfont-jishiben(1).png"];
        
        
    }else if ([model.ftype isEqualToString:@"video"]) {
        
        cell.typeImageView.image = [UIImage imageNamed:@"iconfont-video(1).png"];
        
    }else if ([model.ftype isEqualToString:@"pic"]){
       
        cell.typeImageView.image = [UIImage imageNamed:@"iconfont-sanheng.png"];
    }else {
        cell.typeImageView.image = [UIImage imageNamed:@"iconfont-sanheng.png"];
    }
    cell.titleLabel.text = model.content[@"title"];
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:model.content[@"url"]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];

    cell.timeLabel.text = [TimeTool Whattime:model.content[@"time"]];
   
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.SquareArr.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SquareModel *model =  self.SquareArr[indexPath.row];
    ZWBWebVC *webVC = [[ZWBWebVC alloc] init];
    webVC.model = model;
    [self.navigationController pushViewController:webVC animated:YES];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return VIEW_HEIGHT / 3;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self network];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
